package com.hdaccp.xingmu.controller;

import com.hdaccp.xingmu.dao.*;
import com.hdaccp.xingmu.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/house")
public class HouseCon {

    @Autowired
    private house housedao;
    @Autowired
    private userdetailsDao userdetailsDao;
    @Autowired
    private OrderDao orderDao;

    @Autowired
    private EntityManager entityManager;

    @Autowired
    private SrecordDao srecordDao;

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(String name ,String shu,String type,String number,int pageNumber,int pageSize){



        int type2 = Integer.parseInt(type);
        Pageable pageable = new PageRequest((pageNumber-1),pageSize);
        Specification specification = new Specification<House>(){
            @Override
            public Predicate toPredicate(Root<House> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<Predicate>();
                if(type2!=-1){
                    predicates.add(criteriaBuilder.equal(root.get("typeb").get("id"), type2));
                }
                if(shu!=null){
                    String tt = shu.substring(2,3);
                    if(tt.equals("楼")){
                        predicates.add(criteriaBuilder.like(root.get("sale").get("name"), "%"+shu+"%"));
                    }else if(tt.equals("元")){
                        predicates.add(criteriaBuilder.like(root.get("danyuan").get("name"), "%"+shu+"%"));
                    }
                }
                if(!number.equals("-1")){
                    predicates.add(criteriaBuilder.like(root.get("doornumber"),"%"+number+"%"));
                }


                Predicate[] p = new Predicate[predicates.size()];
                return criteriaBuilder.and(predicates.toArray(p));
            }
        };

        List<House> all2 = housedao.findAll(specification);
        Page<House> all = housedao.findAll(specification,pageable);
        int zhong = all2.size();
        System.out.println(all2.size());
        List<House> list = all.getContent();


        Map<String,Object> map=new HashMap<String,Object>();
        map.put("total",zhong);
        map.put("rows",list);

        return map;
    }

    @RequestMapping("/shoufei")
    @ResponseBody
    public List<House> list2(){
        Specification specification = new Specification<House>(){
            @Override
            public Predicate toPredicate(Root<House> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<Predicate>();
                Predicate[] p = new Predicate[predicates.size()];
                return criteriaBuilder.and(predicates.toArray(p));
            }
        };

        List<House> all2 = housedao.findAll(specification);

        return all2;
    }
    @RequestMapping("/menpai")
    @ResponseBody
    public List<House> menpaihao(){

        List<House> houselist = housedao.findAll();

        return houselist;
    }

    @RequestMapping("/upd1")
    @ResponseBody
    public Map<String,Object> upd(String number,Long yin){

        House house1 = housedao.findById(yin).get();

        Typeb typeb = new Typeb();
        typeb.setId(2);

        house1.setTypeb(typeb);

        house1 = housedao.save(house1);


        return list("-1",null,"-1","-1",1,15);
    }

    @RequestMapping("/numbercha")
    @ResponseBody
    public List<House> list(String number){

//        树形菜单
        Specification specification = new Specification<House>(){
            @Override
            public Predicate toPredicate(Root<House> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<Predicate>();
                if(!number.equals("-1")){
                    predicates.add(criteriaBuilder.like(root.get("doornumber"),"%"+number+"%"));
                }
                Predicate[] p = new Predicate[predicates.size()];
                return criteriaBuilder.and(predicates.toArray(p));
            }
        };

        List<House> list = housedao.findAll(specification);
        return list;
    }



    @RequestMapping("/numbercha1")
    @ResponseBody
    public List<Srecord> list2(int number){

//        树形菜单
        Specification specification = new Specification<Srecord>(){
            @Override
            public Predicate toPredicate(Root<Srecord> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<Predicate>();
                if(number!=-1){
                    predicates.add(criteriaBuilder.equal(root.get("house").get("id"),number));
                    predicates.add(criteriaBuilder.equal(root.get("type"),1));
                }
                Predicate[] p = new Predicate[predicates.size()];
                return criteriaBuilder.and(predicates.toArray(p));
            }
        };
        List<Srecord> list = srecordDao.findAll(specification);
        return list;
    }


    @RequestMapping("/ttt")
    @ResponseBody
    public List<House> ll(){
        List<House> list2 = housedao .findAll();

        return list2;
    }


    //房产信息表
    @RequestMapping("/Qtzfangchanxxbiao")
    public String Qtzfcxxb(){
        return "WEB-INF/jsp/fangchanxinxibiao";
    }
    /**
     *以下房屋表
     */
    @RequestMapping("/houseAll")
    @ResponseBody
    public Map<String,Object> houseAll(String doornumber,int pageSize,int pageNumber) {

        //分页
        String jpal = "from House where 1=1";
        if (doornumber != null && !doornumber.equals("")) {
            jpal = jpal + " and doornumber=" + "'" + doornumber + "' ";
        }
        Query qu = entityManager.createQuery(jpal);
        //起始页书
        qu.setFirstResult((pageNumber - 1) * pageSize);
        //结束页数
        qu.setMaxResults(pageSize);

        //查询多少条数据
        String jpa = "select count(s) from House s";

        Query q = entityManager.createQuery(jpa);

        Long count = (Long) q.getSingleResult();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", count);
        map.put("rows", qu.getResultList());
        return map;
    }



    //业主信息表
    @RequestMapping("/Qyzxx")
    public String Qyzxx(){
        return "WEB-INF/jsp/yezhuxinxi";
    }


    @RequestMapping("/list2")
    @ResponseBody
    public Map<String,Object> list2(int name ,String shu,String type,String number,int pageNumber,int pageSize){

        System.out.println(name);

        int type2 = Integer.parseInt(type);

        Pageable pageable = new PageRequest((pageNumber-1),pageSize);
        Specification specification = new Specification<House>(){
            @Override
            public Predicate toPredicate(Root<House> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<Predicate>();
                if(type2!=-1){
                    predicates.add(criteriaBuilder.equal(root.get("typeb").get("id"), type2));
                }
                if(shu!=null){
                    String tt = shu.substring(2,3);
                    if(tt.equals("楼")){
                        predicates.add(criteriaBuilder.like(root.get("sale").get("name"), "%"+shu+"%"));
                    }else if(tt.equals("元")){
                        predicates.add(criteriaBuilder.like(root.get("danyuan").get("name"), "%"+shu+"%"));
                    }
                }
                if(!number.equals("-1")){
                    predicates.add(criteriaBuilder.like(root.get("doornumber"),"%"+number+"%"));
                }
                if(name!=-1){
                    predicates.add(criteriaBuilder.equal(root.get("userdetails").get("property").get("id"),name));
                }

                Predicate[] p = new Predicate[predicates.size()];
                return criteriaBuilder.and(predicates.toArray(p));
            }
        };

        List<House> all2 = housedao.findAll(specification);
        Page<House> all = housedao.findAll(specification,pageable);
        int zhong = all2.size();
        System.out.println(all2.size());
        List<House> list = all.getContent();


        Map<String,Object> map=new HashMap<String,Object>();
        map.put("total",zhong);
        map.put("rows",list);

        return map;
    }




    /**
     * 房屋添加
     */
    @RequestMapping("/tianjiaadd")
    @ResponseBody
    public Map<String,Object> addtainjia(House house){
        housedao.save(house);
        return houseAll(null,11,1);
    }
    /**
     * 房屋删除
     */
    @RequestMapping("/fwdel")
    @ResponseBody
    public Map<String,Object> fwdel(Long id){
        housedao.deleteById(id);
        return houseAll(null,11,1);
    }
    /**
     * 房屋修改
     * @param house
     * @return
     */
    @RequestMapping("/updxg")
    @ResponseBody
    public Map<String,Object> updxiu(House house){
        housedao.save(house);
        return houseAll(null,11,1);
    }




        @RequestMapping("/menpai2")
        @ResponseBody
        public List<Jfxq> quanbujinayi() {
            int type=2;
            String jpal="FROM House WHERE type='"+type+"'";
            Query qu = entityManager.createQuery(jpal);

            List<Jfxq> list = qu.getResultList();

            return list;


        }
}
